package com.yicong.ants.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.yicong.ants.R;
import com.yicong.ants.utils.L;

/**
 * 抽象的 PopupWindow 基类
 */
public abstract class BasePopupWindow extends PopupWindow {
    private static final String TAG = BasePopupWindow.class.getSimpleName();
    public Activity mContext;
    public View mView;
    // 是否设置背景颜色，默认是设置背景灰度的
    public boolean isBackGround = true;
    /**
     * 屏幕灰度级别
     */
    private float mLevel = 0.4f;

    public BasePopupWindow(Context context) {
        super(context);
        if (null == context) {
            L.e(TAG, "BasePopupWindow context 为空");
            return;
        }
        if (context instanceof Activity) {
            this.mContext = (Activity) context;
        }
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setFocusable(true);
        this.setBackgroundDrawable(null);
        initAnimation();
        initSetting();
        initView();
        initListener();
        setContentView(mView);
    }

    public void setLevel(float mLevel) {
        this.mLevel = mLevel;
    }

    /**
     * 初始化 PopupWindow 的设置
     */
    public abstract void initSetting();

    /**
     * 初始化布局
     */
    public abstract void initView();

    /**
     * 初始化监听事件
     */
    public void initListener() {
    }

    private void setBackGroundLevel(float level) {
        Window window = mContext.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.alpha = level;
        window.setAttributes(params);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (isBackGround) {
            setBackGroundLevel(1.0f);
        }
    }

    @Override
    public void showAsDropDown(View anchor) {
        this.showAsDropDown(anchor, 0, 0);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        this.showAsDropDown(anchor, xoff, yoff, Gravity.TOP | Gravity.START);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        initBackGroundLevel();
        initAfterShow();
    }

    // 在显示之后的初始化
    protected void initAfterShow() {
    }

    /**
     * 初始化背景颜色灰度
     */
    private void initBackGroundLevel() {
        if (isBackGround) {
            setBackGroundLevel(mLevel);
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        initBackGroundLevel();
        initAfterShow();
    }

    /**
     * 设置默认的动画，需要设置其他动画直接重写并删除 super 实现即可
     */
    public void initAnimation() {
        setAnimationStyle(R.style.MyPopupWindow_translate_style);
    }

}
